//
//  SecurityUtil.h
//  Smile
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 冷悦. All rights reserved.
//

#import "AESUtil.h"
#import "NSData+AES.h"

@implementation AESUtil

#pragma mark - AES加密
//指定加密的密匙进行AES加密
+(NSString *)encryptAESData:(NSString *)string withKey:(NSString *)key andIV:(NSString *)iv{
    //将nsstring转化为nsdata
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //使用密码对nsdata进行加密
    NSData *encryptedData = [data AES128EncryptWithKey:key gIv:iv];
    //返回进行base64进行转码的加密字符串
    
    return [encryptedData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

#pragma mark - AES解密
+(NSString *)decryptAESData:(NSString *)string withKey:(NSString *)key andIV:(NSString *)iv{
    //base64解密
    NSData *decodeBase64Data= [[NSData alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
    //使用密码对data进行解密
    NSData *decryData = [decodeBase64Data AES128DecryptWithKey:key gIv:iv];
    //将解了密码的nsdata转化为nsstring
    NSString *str = [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];
    return str;
}

@end
